package at.spengergasse.abifs.SDJPATest.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import at.spengergasse.abifs.SDJPATest.model.Athlet;
import at.spengergasse.abifs.SDJPATest.model.TrainingGroup;
import at.spengergasse.abifs.SDJPATest.repository.AthletRepository;

@Service("athletService")
public class AthletServiceImpl implements AthletService
{
	@Autowired
	private AthletRepository athletrepository;

	@PersistenceContext
    private EntityManager em;
	
	@Override
	@Transactional
	public void save(Athlet athlet) 
	{
		athletrepository.save(athlet);
	}

	
	@Override
	@Transactional
	public void delete(Athlet athlet) 
	{
		athletrepository.delete(athlet);	
	}

	@Override
	@Transactional(readOnly=true)
	public Athlet findByID(Long a_id) 
	{
		return athletrepository.findByID(a_id);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Athlet> findAllAthlets() 
	{
		return athletrepository.findAll();
	}


	@Override
	public List<Athlet> findByTrainingGroup(TrainingGroup trainingGroup) 
	{
		TypedQuery<Athlet> query = em.createQuery("select a from Athlet a where a.trainingGroup = ?1", Athlet.class);
		query.setParameter(1, trainingGroup);
		return query.getResultList();
	}

	
	
}
